<template>
  <div class="app-container">
    <el-form class="formWidth" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="安置方式" prop="arrangeWay">
        <el-select v-model="queryParams.arrangeWay" placeholder="请选择安置方式" clearable size="small">
          <el-option
            v-for="dict in arrangeWayOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="安置地点" prop="arrangePlace">
        <el-input
          v-model="queryParams.arrangePlace"
          placeholder="请输入安置地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安置日期" prop="arrangeDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.arrangeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择安置日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单位性质" prop="arrangeUnitType">
        <el-select v-model="queryParams.arrangeUnitType" placeholder="请选择安置单位性质" clearable size="small">
          <el-option
            v-for="dict in arrangeUnitTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单位名称" prop="arrangeUnitName">
        <el-input
          v-model="queryParams.arrangeUnitName"
          placeholder="请输入安置单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manager:retirefm_2:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manager:retirefm_2:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manager:retirefm_2:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['manager:retirefm_2:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="retirefm_2List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" :show-overflow-tooltip="true" align="center" prop="soldierName" />
      <el-table-column label="安置方式" :show-overflow-tooltip="true" align="center" prop="arrangeWay" :formatter="arrangeWayFormat" />
      <el-table-column label="安置地点" :show-overflow-tooltip="true" align="center" prop="arrangePlace" />
      <el-table-column label="安置日期" :show-overflow-tooltip="true" align="center" prop="arrangeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrangeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位性质" :show-overflow-tooltip="true" align="center" prop="arrangeUnitType" :formatter="arrangeUnitTypeFormat" />
      <el-table-column label="安置单位" :show-overflow-tooltip="true" align="center" prop="arrangeUnitName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manager:retirefm_2:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manager:retirefm_2:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退役军人_安置信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form class="formWidth" ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="身份证号" prop="soldierId" v-if="!isUpdate">
          <el-select
            v-model="form.soldierId"
            filterable
            remote
            reserve-keyword
            placeholder="输入身份证号系统自动匹配"
            :remote-method="remoteMethod"
            :loading="idLoading"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="安置方式" prop="arrangeWay">
          <el-select v-model="form.arrangeWay" placeholder="请选择安置方式">
            <el-option
              v-for="dict in arrangeWayOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安置地点" prop="arrangePlace">
          <el-input v-model="form.arrangePlace" placeholder="请输入安置地点" />
        </el-form-item>
        <el-form-item label="安置日期" prop="arrangeDate">
          <el-date-picker clearable size="small"
            v-model="form.arrangeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择安置日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单位性质" prop="arrangeUnitType">
          <el-select v-model="form.arrangeUnitType" placeholder="请选择安置单位性质">
            <el-option
              v-for="dict in arrangeUnitTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安置单位" prop="arrangeUnitName">
          <el-input v-model="form.arrangeUnitName" placeholder="请输入安置单位名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRetirefm_2, getRetirefm_2, delRetirefm_2, addRetirefm_2, updateRetirefm_2, exportRetirefm_2 } from "@/api/toCadre/toCadre_retirefm_2";
import { getBaseInfoByID } from "@/api/manager/retirefm_1";

export default {
  name: "Retirefm_2",
  data() {
    return {
      idLoading: false,
      // 身份证列表
      options: [],
      isUpdate:false,

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退役军人_安置信息表格数据
      retirefm_2List: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 安置方式字典
      arrangeWayOptions: [],
      // 安置单位性质字典
      arrangeUnitTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        arrangeWay: null,
        arrangePlace: null,
        arrangeDate: null,
        arrangeUnitType: null,
        arrangeUnitName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        soldierId: [
          {
            required: true,
            message: "军人身份证号不能为空",
            trigger: "blur",
          },
        ],
        arrangeWay: [
          { required: true, message: "安置方式不能为空", trigger: "change" }
        ],
        arrangePlace: [
          { required: true, message: "安置地点不能为空", trigger: "blur" }
        ],
        arrangeDate: [
          { required: true, message: "安置日期不能为空", trigger: "blur" }
        ],
        arrangeUnitType: [
          { required: true, message: "安置单位性质不能为空", trigger: "change" }
        ],
        arrangeUnitName: [
          { required: true, message: "安置单位名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("arrange_way").then(response => {
      this.arrangeWayOptions = response.data;
    });
    this.getDicts("arrange_property").then(response => {
      this.arrangeUnitTypeOptions = response.data;
    });
  },
  methods: {
    // 通过身份证查询基本信息
    remoteMethod(id) {
      if (id != "") {
        let params = {
          idNumber: id,
        };
        this.options = [];
        getBaseInfoByID(params)
          .then((res) => {
            if (res.rows == null || res.rows == undefined) {
              this.options = [];
              return;
            }
            this.options = res.rows.map((item) => {
              return {
                value: item.id,
                label: item.idNumber,
              };
            });
          })
          .catch((err) => console.error(err));
      } else {
        this.options = [];
      }
    },
    /** 查询退役军人_安置信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.segment = "2";
      listRetirefm_2(this.queryParams).then(response => {
        this.retirefm_2List = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 安置方式字典翻译
    arrangeWayFormat(row, column) {
      return this.selectDictLabel(this.arrangeWayOptions, row.arrangeWay);
    },
    // 安置单位性质字典翻译
    arrangeUnitTypeFormat(row, column) {
      return this.selectDictLabel(this.arrangeUnitTypeOptions, row.arrangeUnitType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        soldierId: null,
        arrangeWay: null,
        arrangePlace: null,
        arrangeDate: null,
        arrangeUnitType: null,
        arrangeUnitName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isUpdate = false;
      this.reset();
      this.open = true;
      this.title = "添加退役军人_安置信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isUpdate = true;
      this.reset();
      const id = row.id || this.ids
      getRetirefm_2(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退役军人_安置信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRetirefm_2(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.segment = "2"
            addRetirefm_2(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该项数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRetirefm_2(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有退役军人_安置信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportRetirefm_2(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
